<?php
require_once 'class/db/dbFase.php';
require_once 'class/db/dbFaseStap.php';

/**
 * Description of Fase
 * 
 * All functionality related to a project fase 
 *      is provided from here.
 *
 * @author GHoogendoorn
 * @version 0.1
 * 
 * Version history
 * 0.1  GHoogendoorn    Initial version
 */
class Fase {
    
    private $fase_id;
    private $fase;
    private $fase_kleur = 'ffffff';
    private $fase_order ='';
    /**
     *
     * @var array [fase_stap_id] = fase_stap_naam 
     */
 /**   private $fase_stap_array = array(); **/
    /**
     *
     * @var int Pointer for the fase_stap_array
     */
    private $fase_stap_pointer = 0;
    
    /**
     *
     * @var array Provides a lookup array for the step idx's 
     */
    private $fase_stap_pointer2idx_array = array();
    /**
     *
     * @var array() Array containing the known steps for this fase.
     */
    private $fase_step_array = array();
    
    /**
     *
     * @var array Array containing the step ids
     */
    private $fase_step_id_array = array();
    
    
    public function __construct($id = '') {
        //Initialisatie
        if ( !empty($id) ){
           
            // Load class attributes
            $this->selectById($id);
        }
        
        $this->resetFaseStapPointer();
        
        
        //*
        echo __FILE__.__LINE__.__FUNCTION__.'<br />';
        echo '<pre>';
        var_dump($id);
        echo '</pre>';
        //*/
    }
    
    public function delete(){
        /**
         * Test existing id.
         */
        if ( strlen($this->fase_id)<=0){
            return false;
        } 
        $db = new DbFase($this->fase_id);
        $db->delete();
        $this->reset();
    }
    
    public function getFase_id() {
        return $this->fase_id;
    }

    public function setFase_id($fase_id) {
        $this->fase_id = $fase_id;
        $this->selectById($fase_id);
    }

    public function getFase() {
        return $this->fase;
    }

    public function setFase($fase) {
        $this->fase = $fase;
    }
    
    /**
     *
     * @return int Number of items in fase stap array
     */
    public function getFaseStepCount(){
        
        return count( $this->fase_step_array );
    }

    public function getFaseKleur() {
        return $this->fase_kleur;
    }

    public function setFaseKleur($fase_kleur) {
        $this->fase_kleur = $fase_kleur;
    }

    public function getFaseStapArray() {
        return $this->fase_stap_array;
    }
    
    public function getNextStep(){
        
        /**
         * If first time retrieve project list.
         */
        if ( (!is_array($this->fase_step_array)) || (empty($this->fase_step_array)) ){
            
            $this->loadFaseStapArray();
            
        }

        
        /**
         * Return the current project.
         */
        if (array_key_exists($this->fase_stap_pointer, $this->fase_stap_pointer2idx_array)){
            
            $current_pointer = $this->fase_stap_pointer;
            /**
             * Increase the project pointer 
             */
            $this->fase_stap_pointer++;
            
            /**
             * return the previous idx of the fase step list 
             */
            $return_array = array();
            $idx = $this->getFaseStepIndex($current_pointer);
            $return_array['id'] = $idx;
            $return_array['name'] = $this->fase_step_id_array[ $idx ];
            
            return $return_array;
        }
        
        else {
            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';
            var_dump($this);
            echo '</pre>';
            //*/
        }

        /**
         *  Last entry or invalid entry
         * 
         *  Reset the pointer and return FALSE
         */
        $this->resetFaseStapPointer();
        
        return false;
        
    }
    
    /**
     * Load all fase steps that belong to this fase.
     * @param int $id 
     * 
     */
    public function loadFaseStapArray(){
        try {
            if ( empty($this->fase_id)){
                throw new Exception ('Invalid class ID');
            }
            $fase_stap = new FaseStap();
            
            
            $this->fase_step_id_array = $fase_stap->getFaseStapIdList($this->fase_id);
 
            /** 
             * Load based on the ID list all steps for this object
             */
            foreach( $this->fase_step_id_array as $idx => $value){
                
                $this->fase_step_array[$idx] = new FaseStap($idx);
            }
            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';

            var_dump($this);
            echo '</pre>';
            //*/
            
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
        
        
        $this->resetFaseStapPointer();
        
       
    }
/** OBSOLETE -> Step object array
    public function setFaseStapArray($fase_stap_array) {
        try {
            if (is_array($fase_stap_array) ){
                $this->fase_stap_array = $fase_stap_array;
            } else {
                throw new Exception('Input is not an array');
            }
        
        } catch (Exception $e) {
            trigger_error($e->getMessage());
        }
    }
**/
    /**
     * Save the class vars.<br />
     * When saved the generated id is retrieved from the DB class.
     * <br />
     * trigger_error called on error.
     */
    public function save(){
        try {
                if (!empty($this->fase_id)){
                    throw new Exception('Class already saved');
                }
                if( empty($this->fase)          || 
                    empty($this->fase_kleur)    || 
                    empty($this->fase_order))   {
                    
                   throw new Exception('missing class vars');
                }
                
                $db = new DbFase();
                $db->save($this->fase, $this->fase_kleur, $this->fase_order);
                
                /**
                 * Store the generated id.
                 */
                $this->fase_id = $db->getId();
               
            } catch (Exception $e) {
                if ( $e->getCode() == E_WARNING){
                    echo "Warning";
                } else {
                    trigger_error($e->getTraceAsString().'\n'.$e->getMessage());
                }
            }
        
    }
    
    private function getFaseStepIndex($position){
        return $this->fase_stap_pointer2idx_array[$position];
    }
    
    private function selectById($id){
        try {
            if ( empty( $id )){
                throw new Exception('Empty ID');
            }
            if ( !is_numeric($id)){
                throw new Exception('Invalid ID');
            }
            
            $db = new DbFase($id);
            $this->fase = $db->getFase();
            $this->fase_kleur = $db->getFase_color();
            $this->fase_id = $id;
            $this->fase_order = $db->getOrder();
            
            $this->loadFaseStapArray();
            
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }
    
    private function reset(){
        $this->fase = '';
        $this->fase_id = '';
        $this->fase_kleur = '';
        $this->fase_order = '';
        $this->fase_stap_array = array();
        $this->resetFaseStapPointer();
        $this->fase_stap_pointer2idx_array = array();
        $this->fase_step_array = array();
    }
    
    private function resetFaseStapPointer(){
        $this->fase_stap_pointer = 0;
        
        /** 
         * (RE)create the pointer 2 idx list.
         */
        $this->fase_stap_pointer2idx_array = array();
        /**
         * Create an array containing all keys index should start from 0
         */
        $this->fase_stap_pointer2idx_array = array_keys($this->fase_step_id_array);
        
        /*
        echo __FILE__.__LINE__.__FUNCTION__.'<br />';
        echo '<pre>';
        var_dump($this->fase_stap_pointer2idx_array);
        echo '</pre>';
        //*/
        
    }



}

class TestFase {

    public function TestFase() {
        try {
            $test = new Fase(1);

            
            //*
            echo __FILE__.__LINE__.__FUNCTION__.'<br />';
            echo '<pre>';
            var_dump($test);
            echo '</pre>';
            //*/
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
    }

}

?>
